package ltd.followkba.comment;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import ltd.followkba.comment.bean.BSongInfo;
import ltd.followkba.comment.bean.BUserInfo;
import ltd.followkba.comment.utils.CFile16bDbs;
import main.MAPI;

import static ltd.followkba.comment.MKCC.APPC_GET_RANK_LIST;
import static ltd.followkba.comment.MKCC.APPC_GET_SONG_INFO;
import static ltd.followkba.comment.MKCC.APPC_GET_SONG_LIST;
import static ltd.followkba.comment.MKCC.APPC_GET_USER_INFO;
import static ltd.followkba.comment.MKCC.APPC_USER_LOGIN;
import static ltd.followkba.comment.MKCC.cKCB_SW_YANCHANG;

public class KgYanChang extends KgAppBase {
    public List<BUserInfo> mRankUserInfo = null;
    public int mUserIDCounter = 0;
    public KgYanChang(){
        SwitchAppType(cKCB_SW_YANCHANG);
MAPI.DELETE_FILE(gsCommmentDbsFile);
MAPI.DELETE_FILE(gsRankDbsFile);
    }

    @Override
    public BUserInfo GetUserFullInfo(String mixAccount, BUserInfo beforeUserInfo){
//        if ("".equals(mixAccount)){
//            mixAccount = gsUserID;
//        }
//        else {
//            int g4Local_1 = mixAccount.indexOf("#");
//            if (g4Local_1 > 0) {
//                mixAccount = mixAccount.substring(0, g4Local_1);
//            }
//            g4Local_1 = mixAccount.indexOf(",");
//            if (g4Local_1 > 0) {
//                mixAccount = mixAccount.substring(0, g4Local_1);
//            }
//        }
//        BUserInfo BUserInfo = null;
//        ResultNet resultNet;
//        String beforeCounter = "0";
//        if (beforeUserInfo == null) {
//            resultNet = WriteCommand(APPC_GET_USER_INFO, mixAccount, "");
//MSTRING("演唱汇GetUserFullInfo " + mixAccount + " "+resultNet.message);
//            if (resultNet.sucess && resultNet.result.size() > 0) {
//                BUserInfo = (BUserInfo) resultNet.result.get(0);
//            }
//        }
//        else {
//            BUserInfo = beforeUserInfo;
//            beforeCounter = beforeUserInfo.beforeCounter;
//            if ("".equals(beforeCounter)){
//                beforeCounter = "0";
//            }
//        }
//        if (BUserInfo != null) {
//            resultNet = WriteCommand(APPC_GET_SONG_LIST, mixAccount, beforeCounter);
//            if (resultNet.result != null && resultNet.result.size() > 0) {
//// MSTRING(resultNet.result.size());
//                if (BUserInfo.IdS == null) {
//                    BUserInfo.IdS = new ArrayList<>();
//                }
//                BUserInfo.IdS.addAll(resultNet.result);
//                BUserInfo.beforeCounter = resultNet.message;
//
//                if (BUserInfo.WorksQty == resultNet.result.size()){  // 结束
//                    BUserInfo.beforeCounter = "";
//                }
//            }
//            return BUserInfo;
//        }
//MSTRING("mRankUserInfo "+mRankUserInfo +" "+mRankUserInfo.size());
        if (mRankUserInfo != null && mRankUserInfo.size() > 0){
            int g4Local_1 = 0;
            BUserInfo BUserInfo;
//MSTRING("mRankUserInfo"+mixAccount);
            do {
                BUserInfo = mRankUserInfo.get(g4Local_1);
//MSTRING(g4Local_1+" "+BUserInfo.UserIdS);
                if (mixAccount.equals(BUserInfo.userID)){
                    return BUserInfo;
                }
            } while (++g4Local_1 < mRankUserInfo.size());
        }
        return null;
    }
    @Override
    public BUserInfo GetUserFullInfo(String mixAccount){
//        BUserInfo BUserInfo = GetUserFullInfo(mixAccount, null);
//MSTRING("GetUserFullInfo 1 "+BUserInfo);
//        if (BUserInfo != null && BUserInfo.IdS != null) {
//MSTRING("IdS "+ BUserInfo.IdS.get(0).toString());
//
//            BUserInfo.NewWorks = GetSongInfo(BUserInfo.IdS.get(0));
//        }
        if (mRankUserInfo != null && mRankUserInfo.size() > 0){
            int g4Local_1 = 0;
            BUserInfo BUserInfo;
            do {
                BUserInfo = mRankUserInfo.get(g4Local_1);
                if (mixAccount.equals(BUserInfo.userID)){
                    return BUserInfo;
                }
            } while (++g4Local_1 < mRankUserInfo.size());
        }
        return null;
    }
    @Override
    public BSongInfo GetSongInfo(String SongID){
        if (!"".equals(SongID)) {
            ResultNet resultNet = WriteCommand(APPC_GET_SONG_INFO, SongID, "");
            if (resultNet.sucess && resultNet.result.size() > 0) {
                return (BSongInfo)resultNet.result.get(0);
            }
        }
        return null;
    }
    @Override
    public BUserInfo GetValidUseInfo(int gender) {            // 0=需要女用户，1=需要男用户，0=不指定男女
        String useID;
        BUserInfo BUserInfo;
        int g4Local_1 = 0;
        do {
            useID = GetUserFromRank16b();
//MSTRING("eeeeee22");MSTRING(useID);
            if (!"".equals(useID)){
                BUserInfo = GetUserFullInfo(useID, null);
//MSTRING("3333");MSTRING(BUserInfo);
                if (BUserInfo != null){
                    return BUserInfo;
//                    if (Gender < 0){
//                        return BUserInfo;
//                    }
//                    if (Gender == 1){                           // 0=需要男用户
//                        if ("1".equals(BUserInfo.Gender)){
//                            return BUserInfo;
//                        }
//                    }
//                    else if (!"1".equals(BUserInfo.Gender)){    // 0=需要女用户
//                        return BUserInfo;
//                    }
                }
            }
        } while (++g4Local_1 < 20);
        return null;
    }
    @Override
    public ResultNet GetRankList(){
        SetLongitudeLatitude(0);
        String city = MUTIL.GET_RANK_YAN_CHANG(mRankCounter);
        ResultNet resultNet = WriteCommand(APPC_GET_RANK_LIST, city, "");
        resultNet.message = city;
        return resultNet;
    }

    @Override
    public BUserInfo GetNextPreparatUseID() {
        CFile16bDbs preparatDbs = new CFile16bDbs();
        preparatDbs.setFileName(gsPreparatDbsFile);
        if (preparatDbs.mCounter == 0){
            preparatDbs.close();
            MSTRING("GetNextPreparatUseID 44");
            return null;
        }
        byte gLocal_1;
        String gsLocal_1;
        CFile16bDbs alreadyDbs = new CFile16bDbs();
        preparatDbs.setFileName(gsAlreadyUser);
        BUserInfo BUserInfo;
        while (preparatDbs.mCounter > 0) {
            gsLocal_1 = preparatDbs.readUserID(preparatDbs.mOutput);
            if (++preparatDbs.mOutput == 1024){
                preparatDbs.mOutput = 0;
            }
            --preparatDbs.mCounter;
            preparatDbs.writeOutput();
            preparatDbs.writeCounter();
            if (!IsSkipUserList(gsLocal_1)) {
                BUserInfo = GetUserFullInfo(gsLocal_1);
                if (BUserInfo != null && BUserInfo.songIDs != null && BUserInfo.songQty > 0 && BUserInfo.newSong != null) {
//                    gLocal_1 = (byte)alreadyDbs.GetUserID4Byte(g4Local_3);
//                    //MDWORD(0x98);MSTRING(gLocal_2);
//                    if (gLocal_1 == -1 || (MKCS.gDayOfMonth != gLocal_1)) {
//                        alreadyDbs.SetUserID4Byte(g4Local_3, MKCS.gDayOfMonth);
//                        alreadyDbs.close();
//                        preparatDbs.close();
////MSTRING(String.format("%d/%d找到评论ID：%s(%s %d/%d)", g4FindCommCount, g4FindCommLength, gsLocal_1, MUTIL.RD_PRIVATE_STRING("name", gsRankFile), MUTIL.RD_PRIVATE_VALUE("counter", gsRankFile), MUTIL.RD_PRIVATE_VALUE("length", gsRankFile)));
//                        MSTRING("GetNextPreparatUseID B "+preparatDbs.mOutput+" "+preparatDbs.mInput+" " + preparatDbs.mCounter);
//                        return BUserInfo;
//                    }
                }
//MSTRING(String.format("%d/%d跳过ID：%s(%s %d/%d) %d", g4FindCommCount, g4FindCommLength, gsLocal_1, MUTIL.RD_PRIVATE_STRING("name", gsRankFile), MUTIL.RD_PRIVATE_VALUE("counter", gsRankFile), MUTIL.RD_PRIVATE_VALUE("length", gsRankFile), MKCS.gDayOfMonth));
            }
        }
        preparatDbs.close();
        alreadyDbs.close();
        MSTRING("GetNextPreparatUseID C "+preparatDbs.mOutput+" "+preparatDbs.mInput+" " + preparatDbs.mCounter);
        return null;
    }
    @Override
    public boolean IsNeedPreparatUseID() {
        CFile16bDbs preparatDbs = new CFile16bDbs();
        preparatDbs.setFileName(gsPreparatDbsFile);
        int g4Local_1 = preparatDbs.mCounter;
        preparatDbs.close();
        if (g4Local_1 < 200){
            return true;
        }
        return false;
    }
    @Override
    public void CreatePreparatUseID() {
        CFile16bDbs preparatDbs = new CFile16bDbs();
        preparatDbs.setFileName(gsPreparatDbsFile);
        int g4Local_1 = 0;
        int g4Local_2 = preparatDbs.mCounter;
        String gsUserID;
//MSTRING("CreateReadyUseID A " + preparatDbs.mOutput + " "+preparatDbs.mInput + " "+preparatDbs.mCounter+" " + gsPreparatDbsFile);
        while (NetworkError == 0 && g4Local_1 < 100) {
            gsUserID = GetUserFromRank16b();
//MSTRING(g4UserID + " " + readyDbs.mInput);
            if (!"".equals(gsUserID)) {
                preparatDbs.writeUserID(preparatDbs.mInput, gsUserID);
                if (++preparatDbs.mInput == 1024){
                    preparatDbs.mInput = 0;
                }
                ++preparatDbs.mCounter;
            }
            ++g4Local_1;
        }
//MSTRING("CreateReadyUseID B " + preparatDbs.mOutput + " "+preparatDbs.mInput + " "+preparatDbs.mCounter);
        MKCS.gsWorkStatus[AppType] = String.format("预备数据：出%d入%d计%d/%d", preparatDbs.mOutput, preparatDbs.mInput, g4Local_2, preparatDbs.mCounter);
        preparatDbs.writeInput();
        preparatDbs.writeCounter();
        preparatDbs.close();
    }

    public String GetUserFromRank16b() {
        if (mUserIDCounter == 0){
            GetRankList();
            if (mRankUserInfo != null){
                mUserIDCounter = mRankUserInfo.size();
            }
        }
        if (mUserIDCounter > 0){
            --mUserIDCounter;
            if (mUserIDCounter < mRankUserInfo.size()) {
                return mRankUserInfo.get(mUserIDCounter).userID;
            }

        }
        return "";
    }
//        String gsLocal_1 = "";
////        CFile16bDbs commentDbs = new CFile16bDbs();
////        commentDbs.setFileName(gsCommmentDbsFile);
////MSTRING("GetUserFromRank "+commentDbs.mInput+" "+commentDbs.mOutput);
////        if (commentDbs.mOutput < commentDbs.mInput) {
////            gsLocal_1 = commentDbs.readUserID(commentDbs.mOutput);
////            ++commentDbs.mOutput;
////            commentDbs.writeOutput();
////            commentDbs.close();
////            MSTRING("从评论： "+gsLocal_1+" "+commentDbs.mInput+"/"+commentDbs.mOutput);
////            return gsLocal_1;
////        }
//        CFile16bDbs rankDbs = new CFile16bDbs();
//        rankDbs.setFileName(gsRankDbsFile);
//MSTRING("准备从榜之中读取用户ID "+mRankCounter+" "+rankDbs.mOutput+" "+rankDbs.mInput+" "+gsRankDbsFile);
//        if (rankDbs.mOutput >= rankDbs.mInput) {
//            rankDbs.mInput = 0;
//            rankDbs.mOutput = 0;
//            ResultNet resultNet = GetRankList();
//            if (resultNet.result != null) {
//                WriteListUserIdToFile(resultNet.result, rankDbs);
//            }
//            gsLocal_1 = rankDbs.readUserID(rankDbs.mOutput);
//            ++rankDbs.mOutput;
////            WriteToLogFile("榜中加入 "+rankDbs.mInput);
//            rankDbs.writeInput();
//            rankDbs.writeOutput();
//            rankDbs.close();
//            return gsLocal_1;
//        }
//        gsLocal_1 = rankDbs.readUserID(rankDbs.mOutput);
//        ++rankDbs.mOutput;
//        rankDbs.writeOutput();
//        rankDbs.close();
//
//        BUserInfo BUserInfo = GetUserFullInfo(gsLocal_1);
//MSTRING(gsLocal_1+" aaaaaaa "+rankDbs.mInput+" "+BUserInfo);
//        if (BUserInfo != null && BUserInfo.IdS != null && BUserInfo.WorksQty > 0 && BUserInfo.NewWorks != null) {
//            ResultNet resultNet = GetCommentList(-1, BUserInfo.UserIdS, BUserInfo.NewWorks.IdS, BUserInfo.NewWorks.commentID);
//            if (resultNet.sucess && resultNet.result != null) {
//                commentDbs.mOutput = 0;
//                commentDbs.mInput = 0;
//                WriteListUserIdToFile(resultNet.result, commentDbs);
////MSTRING("bbbbbb "+commentDbs.mInput +"/"+resultNet.result.size());
//                commentDbs.writeInput();
//                commentDbs.writeOutput();
//                commentDbs.close();
//            }
//            MSTRING("从" + mRankCounter + "榜： "+gsLocal_1+" "+commentDbs.mInput+" "+rankDbs.mOutput+"/"+rankDbs.mInput);
//            return gsLocal_1;
//        }
//        return "";
//    }
    public void WriteListUserIdToFile(List<String> topLists, CFile16bDbs cFileDbs){
        cFileDbs.seek(cFileDbs.mInput);
        int g4Local_1 = topLists.size();
        String[]gpLocal_1 = new String[g4Local_1];
        String gsLocal_1;
        int g4Local_2 = 0;
        int g4Local_3 = 0;
        int g4Local_4;
 //       int g4Local_5;
//MSTRING("WriteUserIDToRankFileA " + cFileDbs.mInput + " " + g4Local_1);
        while (g4Local_2 < g4Local_1) {
            gsLocal_1 = topLists.get(g4Local_2);
            g4Local_4 = 0;
            while (g4Local_4 < g4Local_3) {
                if (gpLocal_1[g4Local_4].equals(gsLocal_1)){
                    break;
                }
                ++g4Local_4;
            }
            if (g4Local_4 == g4Local_3) {
                gpLocal_1[g4Local_3++] = gsLocal_1;
                cFileDbs.writeUserID(cFileDbs.mInput, gsLocal_1);
                ++cFileDbs.mInput;
            }
            ++g4Local_2;
        }
//MSTRING("WriteUserIDToRankFileC " + cFileDbs.mInput);
    }
    public ResultNet WriteCommand(int type, String aux1, String aux2){
        String gsLocal_1;
        String gsLocal_2;
        String gsLocal_3;
        int g4Local_1;
        int g4Local_2;
        ResultNet resultNet = new ResultNet();
        switch (type){
            case APPC_USER_LOGIN:
                gsLocal_1 = "/gateway/socialHandler/findRegistered";
                gsLocal_2 = "access_token=" + aux1 + "&channel=huuhoo&device=1&logonDevice=353867058234837&resource=1&type=2&uid=" + aux2 + "3&version=22008";
                gsLocal_1 = WriteToSvr(1, gsLocal_1, gsLocal_2);
//MDWORD(0xa1);MSTRING(gsLocal_1);
                if (MakeStdReturn(gsLocal_1, resultNet) == true) {
                }
                break;
            case APPC_GET_USER_INFO:
//aux1="50bd45a2-83b6-4114-b499-b3fffcfdf34d";
                gsLocal_1 = "/gateway/compositionHandler/playerBrowseComposition";
                gsLocal_2 = "playerId=&CompositionId=" + aux1 + "&loginsessionkey=";
                gsLocal_1 = WriteToSvr(1, gsLocal_1, gsLocal_2);
MSTRING("APPC_GET_USER_INFO 1 "+ aux1 +" "+gsLocal_1);
                if (MakeStdReturn(gsLocal_1, resultNet) == true) {
                    List<BUserInfo> BUserInfos = new ArrayList<>();
                    BUserInfo BUserInfo = new BUserInfo();
MSTRING("APPC_GET_USER_INFO 2 "+ resultNet.nextJason);

//                    gsLocal_2 = MUTIL.GET_JSON_STRING(gsLocal_1, "items");
                    gsLocal_1 = MUTIL.GET_JSON_STRING(resultNet.nextJason, "player");

                    BUserInfo.userID = MUTIL.GET_JSON_STRING(gsLocal_1, "uid");
                    BUserInfo.nickName = MUTIL.GET_JSON_STRING(gsLocal_1, "UserName");
                    BUserInfo.intro = MUTIL.GET_JSON_STRING(gsLocal_1, "signature");
//MDWORD(0xa2);MSTRING(BUserInfo.Intro);MSTRING(BUserInfo.UserName);
                    BUserInfo.headUrl = MUTIL.GET_JSON_STRING(gsLocal_1, "headImgPath");
                    BUserInfo.gender = MUTIL.GET_JSON_STRING(gsLocal_1, "Gender");      // 男=1女=0
                    //BUserInfo.Birthday = MUTIL.GET_JSON_STRING(gsLocal_1, "bornDate");
//MSTRING(BUserInfo.Birthday);
                    BUserInfo.latitude = MUTIL.GET_JSON_STRING(gsLocal_1, "latitude");
                    BUserInfo.longitude = MUTIL.GET_JSON_STRING(gsLocal_1, "longitude");
                    gsLocal_2 = MUTIL.GET_JSON_STRING(gsLocal_1, "City");
                    if (!"".equals(gsLocal_2) && !"null".equals(gsLocal_2)) {
                        g4Local_1 = gsLocal_2.indexOf(' ');
                        if (g4Local_1 > 0){
                            BUserInfo.province = gsLocal_2.substring(0, g4Local_1).replace("省", "");
                            BUserInfo.city = gsLocal_2.substring(g4Local_1+1, gsLocal_2.length()).replace("市", "");
                        }
                        else {
                            BUserInfo.province = gsLocal_2.replace("省", "");
                            BUserInfo.city = gsLocal_2.replace("市", "");
                        }
//MDWORD(0xd2);MSTRING("Province="+BUserInfo.Province+" City="+BUserInfo.City);

//                        String[] params = gsLocal_2.split(" ");
////MDWORD(0xd1);MSTRING(gsLocal_2);MSTRING(params[0]);
//                        if (params[0] != null && !"".equals(params[0])) {
//                            BUserInfo.Province = params[0].replace("省", "");
//                        }

//                        if (params[1] != null &&!"".equals(params[1])) {
//                            BUserInfo.City = params[1].replace("市", "");
//                        }
//MSTRING("0xd5 " + resultTopList.UserName +"&"+ resultTopList.UserIdS +"&"+ resultTopList.Gender +"&"+ resultTopList.Province +"&"+ resultTopList.City + "&" + resultTopList.latitude+ "&"+resultTopList.longitude +"&"+ resultTopList.HeadUrl);
                    }
                    BUserInfo.fansQty = MUTIL.GET_JSON_STRING(gsLocal_1, "funsCount");
                    BUserInfo.careQty = MUTIL.GET_JSON_STRING(gsLocal_1, "focusCount");
                    BUserInfos.add(BUserInfo);
                    resultNet.result = BUserInfos;
                }
                break;
            case APPC_GET_SONG_LIST:
                // /gateway/compositionHandler/playerBrowseComposition
                gsLocal_1 = "/gateway/userHandler/findMessageByPlayer";
                gsLocal_2 = "uid=" + aux1 + "&playerId=&loginsessionkey=";
                gsLocal_1 = WriteToSvr(1, gsLocal_1, gsLocal_2);
//MDWORD(0xa2);MSTRING(gsLocal_1);
                if (MakeStdReturn(gsLocal_1, resultNet) == true) {
                    List<String> ACommSongInfos = new ArrayList<>();

                    try {
                        gsLocal_3 = MUTIL.GET_JSON_STRING(resultNet.nextJason, "compositionList");
                        JSONArray jsonArray = new JSONArray(gsLocal_3);
                        g4Local_1 = jsonArray.length();
                        g4Local_2 = 0;
                        while (g4Local_2 < g4Local_1) {
                            JSONObject jsonobject = jsonArray.getJSONObject(g4Local_2);
                            ACommSongInfos.add(jsonobject.getString("uid"));
                            ++g4Local_2;
                        } ;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    resultNet.result = ACommSongInfos;
                }
                break;
            case 88:
                // /gateway/compositionHandler/playerBrowseComposition
                gsLocal_1 = "/gateway/userHandler/findMessageByPlayer";
                gsLocal_2 = "uid=" + aux1 + "&playerId=&loginsessionkey=";
                gsLocal_1 = WriteToSvr(1, gsLocal_1, gsLocal_2);
//MDWORD(0xa2);MSTRING(gsLocal_1);
                if (MakeStdReturn(gsLocal_1, resultNet) == true) {
                    List<BSongInfo> BSongInfos = new ArrayList<>();
                    try {
                        gsLocal_3 = MUTIL.GET_JSON_STRING(resultNet.nextJason, "compositionList");
                        JSONArray jsonArray = new JSONArray(gsLocal_3);
                        g4Local_1 = jsonArray.length();
                        //g4Local_1=5;
//MDWORD(0xaa);MSTRING(g4Local_1);
                        g4Local_2 = 0;
                        int g4Local_3;
                        while (g4Local_2 < g4Local_1) {
                            JSONObject jsonobject = jsonArray.getJSONObject(g4Local_2);
                            BSongInfo BSongInfo = new BSongInfo();
                            BSongInfo.songID = jsonobject.getString("uid");
                            BSongInfo.songName = jsonobject.getString("WorksName");
                            BSongInfo.intro = jsonobject.getString("remark");
                            BSongInfo.path = jsonobject.getString("audioPath");
                            BSongInfo.isMV = jsonobject.getString("mediaType");
                            BSongInfo.duration = jsonobject.getString("playedTimes");

                            BSongInfo.commentQty = jsonobject.getString("commentCount");
                            BSongInfo.shareQty = jsonobject.getString("shareCount");
                            BSongInfo.listenQty = jsonobject.getString("showToCount");
                            BSongInfo.createTime = jsonobject.getString("uploadDate");
                            BSongInfos.add(BSongInfo);
//MDWORD(g4Local_2);
                            ++g4Local_2;
                        } ;
                        resultNet.result = BSongInfos;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case APPC_GET_RANK_LIST :
                if ("全国".equals(aux1)) {
                    gsLocal_2 = "playerId=&count=100&ruleCode=rank_1001&area=&start=0&loginsessionkey=";
                }
                else {
                    gsLocal_2 = "playerId=&count=100&ruleCode=rank_1002&area=" + URLEncoder.encode(aux1) + "&start=0&loginsessionkey=";
                }
                gsLocal_1 = "/gateway/compositionHandler/findComposition2";
                gsLocal_1 = WriteToSvr(1, gsLocal_1, gsLocal_2);
             //   MAPI.BUFF8_WRITE_FILE(0, gsLocal_1.getBytes().length, 0, gsLocal_1.getBytes(), gsWorkPath+aux1+".txt");
                if (MakeStdReturn(gsLocal_1, resultNet) == true) {
                    List<String> topLists = new ArrayList<>();
MSTRING("APPC_GET_RANK_LIST " +aux1+" "+resultNet.nextJason);
                    try {
                        gsLocal_3 = MUTIL.GET_JSON_STRING(resultNet.nextJason, "mainList");
//MDWORD(0xaa);
                        JSONArray jsonArray = new JSONArray(gsLocal_3);
                        g4Local_1 = jsonArray.length();
                        g4Local_2 = 0;
                        mRankUserInfo = new ArrayList<>();
                        while (g4Local_2 < g4Local_1) {
                            JSONObject jsonobject = jsonArray.getJSONObject(g4Local_2);
//MSTRING(jsonobject.getString("playerId"));
                            topLists.add(jsonobject.getString("playerId"));

                            BUserInfo BUserInfo = new BUserInfo();
                            BUserInfo.userID = jsonobject.getString("playerId");
                            BUserInfo.nickName = jsonobject.getString("UserName");
                            BUserInfo.headUrl = jsonobject.getString("songPath");
                            BUserInfo.songQty = 1;
                            BUserInfo.newSong = new BSongInfo();
                            BUserInfo.newSong.songID = jsonobject.getString("uid");
                            BUserInfo.songIDs = new ArrayList<>();
                            BUserInfo.songIDs.add(BUserInfo.newSong.songID);
                            BUserInfo.newSong.songName = jsonobject.getString("WorksName");
                            BUserInfo.newSong.createTime = jsonobject.getString("uploadDate");
                            BUserInfo.newSong.audio = jsonobject.getString("audioPath");
                            BUserInfo.newSong.path = jsonobject.getString("AudioUrl");
                            mRankUserInfo.add(BUserInfo);
                            ++g4Local_2;
                        } ;
                        resultNet.result = topLists;
 MSTRING(String.format("找到演唱汇%s榜用户%d个", aux1, g4Local_2));
                    } catch (JSONException e) {
                        e.printStackTrace();
//MDWORD(0xbb);
                    }
                }
                break;
        }
 //       MSTRING(gsLocal_1);
        return resultNet;
    }
    public boolean MakeStdReturn(String gsLocal_1, ResultNet netResult) {
        String gsLocal_2 = MUTIL.GET_JSON_STRING(gsLocal_1, "result");
        if ("success".equals(gsLocal_2)) {
            netResult.nextJason = MUTIL.GET_JSON_STRING(gsLocal_1, "items");
//MDWORD(0xb1);MSTRING(netResult.nextJason);
            return true;
        }
        netResult.message =  "error:" + MUTIL.GET_JSON_STRING(gsLocal_1, "errorCode") + "," + MUTIL.GET_JSON_STRING(gsLocal_1, "errMsg");
        netResult.sucess = false;
        return false;
    }
    public String WriteToSvr(int type, String destUrl, String param) {
        String gsLocal_1 = "";
        BufferedInputStream bis = null;
        HttpURLConnection mHttpUrl=null;
        //    URLConnection mHttpUrl=null;
        URL url = null;
        byte[] gLocal_Buffer = new byte[0x400000+1460];
        try {
            url = new URL("http://app.m-style.com.cn" + destUrl);
            mHttpUrl = (HttpURLConnection) url.openConnection();
            mHttpUrl.setConnectTimeout(30000);
            mHttpUrl.setReadTimeout(30000);
            mHttpUrl.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            mHttpUrl.setRequestProperty("Connection", "Keep-Alive");
            mHttpUrl.setDoInput(true);
            if (type == 0) {
                mHttpUrl.setDoOutput(false);
                mHttpUrl.setUseCaches(false);
                mHttpUrl.setRequestMethod("GET");
            }
            else  {
                mHttpUrl.setDoOutput(true);
                mHttpUrl.setUseCaches(false);
                mHttpUrl.setRequestMethod("POST");
                DataOutputStream dos=new DataOutputStream(mHttpUrl.getOutputStream());
                dos.writeBytes(param);
                dos.flush();                    // 刷新对象输出流，将任何字节都写入潜在的流中（些处为ObjectOutputStream）
                dos.close();                    // 关闭流对象。此时，不能再向对象输出流写入任何数据，先前写入的数据存在于内存缓冲区中,
            }
            int responseCode = mHttpUrl.getResponseCode();
            if (responseCode == 200 || responseCode == 201) {
                bis = new BufferedInputStream(mHttpUrl.getInputStream());
            }
            else {
                bis = new BufferedInputStream(mHttpUrl.getErrorStream());
            }

            int size = 0;
            int g4Local_1 = 0;
            while ((size = bis.read(gLocal_Buffer, g4Local_1, 1460)) != -1) {
                if (size > 0){
                    g4Local_1 += size;
                    if (g4Local_1 >= 0x400000){
                        break;
                    }
                }
            }
            bis.close();
            if (g4Local_1 > 0){
                gsLocal_1 = new String(gLocal_Buffer, 0, g4Local_1, "utf-8");
            }
            mHttpUrl.disconnect();
        } catch (IOException e) {
        }
        return gsLocal_1;
    }

//    public int GetValidUse() {
//        mFileName = gsWorkPath + "TopList.txt";
//        int g4Local_1 = MUTIL.RD_PRIVATE_VALUE("length", mFileName);
//        int g4Local_2 = MUTIL.RD_PRIVATE_VALUE("counter", mFileName);
////g4Local_2=g4Local_1;
//        if (g4Local_1 == 0 || (g4Local_1 > 0 && g4Local_2 >= g4Local_1)){
//            ResultNet netResult = WriteCommand(APPC_READ_TOP, MUTIL.GET_TOP_YAN_CHANG(mTopCounter), "");
//            if (netResult.result != null) {
//                List<ResultTopList> resultTopLists = netResult.result;
//                mCounter = 0;
//                g4Local_1 = resultTopLists.size();
//                if (g4Local_1 > 0) {
//                    do {
//                        ResultTopList resultTopList = resultTopLists.get(mCounter);
//                        ItemToFile("UserIdS", resultTopList.UserIdS);
//                        ItemToFile("UserName", resultTopList.UserName);
//                        ItemToFile("HeadUrl", resultTopList.HeadUrl);
//                        ItemToFile("Gender", resultTopList.Gender);             // 男=1女=0
//                        ItemToFile("Province", resultTopList.Province);
//                        ItemToFile("City", resultTopList.City);
//                        ItemToFile("latitude", resultTopList.latitude);
//                        ItemToFile("longitude", resultTopList.longitude);
////MSTRING("写入:" + resultTopList.UserIdS +"&"+ resultTopList.UserName +"&"+ resultTopList.Gender +"&"+ resultTopList.Province +"&"+ resultTopList.City +"&"+ resultTopList.latitude +"&"+ resultTopList.longitude);
//                    } while (++mCounter < g4Local_1);
//                }
//                MUTIL.WR_PRIVATE_VALUE("length", mCounter, mFileName);
//                MUTIL.WR_PRIVATE_VALUE("counter", 0, mFileName);
//            }
//            ++mTopCounter;
//            MUTIL.WR_PRIVATE_VALUE("TopCount", mTopCounter, mFileName);
//            return 0;
//        }
//        MUTIL.WR_PRIVATE_VALUE("counter", g4Local_2+1, mFileName);
//        return g4Local_2;
//    }
//    public void ItemToFile(String name, String parameter) {
//        String gsLocal_1 = String.format("%d_%s", mCounter, name);
////MSTRING(gsLocal_1+"="+parameter);
//        MUTIL.WR_PRIVATE_STRING(gsLocal_1, parameter, mFileName);
//
//    gsLocal_2 = "playerId=&count=100&ruleCode=rank_1001&area=&start=0&loginsessionkey=";
//    gsLocal_1 = "/gateway/compositionHandler/findComposition2";
//    gsLocal_1 = WriteToSvr(1, gsLocal_1, gsLocal_2);
//    //   MAPI.BUFF8_WRITE_FILE(0, gsLocal_1.getBytes().length, 0, gsLocal_1.getBytes(), gsWorkPath+aux1+".txt");
////MDWORD(0xaa);MSTRING(gsLocal_1);
//    if (MakeStdReturn(gsLocal_1, resultNet) == true) {
//        List<ResultTopList> resultTopLists = new ArrayList<>();
//        try {
//            gsLocal_3 = MUTIL.GET_JSON_STRING(resultNet.nextJason, "mainList");
////MDWORD(0xaa);MSTRING(gsLocal_3);
//            JSONArray jsonArray = new JSONArray(gsLocal_3);
//            g4Local_1 = jsonArray.length();
////g4Local_1=5;
//            g4Local_2 = 0;
//            int g4Local_3;
//            do {
//                JSONObject jsonobject = jsonArray.getJSONObject(g4Local_2);
//                ResultTopList resultTopList = new ResultTopList();
//                resultTopList.UserIdS = jsonobject.getString("playerId");
//                resultTopList.UserName = jsonobject.getString("UserName");
//                resultTopList.WorksName = jsonobject.getString("WorksName");
//                resultTopList.IdS = jsonobject.getString("uid");
//                resultTopList.songPath = jsonobject.getString("AudioUrl");
//                resultTopList.mediaType = jsonobject.getString("mediaType");
//
//                gsLocal_1 = "/gateway/compositionHandler/playerBrowseComposition";
//                gsLocal_2 = "playerId=&CompositionId=" + resultTopList.IdS + "&loginsessionkey=";
//                gsLocal_1 = WriteToSvr(1, gsLocal_1, gsLocal_2);
////MDWORD(0xcc);MSTRING(gsLocal_1);
//                if ("success".equals(MUTIL.GET_JSON_STRING(gsLocal_1, "result"))){
//                    gsLocal_2 = MUTIL.GET_JSON_STRING(gsLocal_1, "items");
//                    gsLocal_1 = MUTIL.GET_JSON_STRING(gsLocal_2, "player");
////MSTRING(gsLocal_1);
//                    resultTopList.HeadUrl = MUTIL.GET_JSON_STRING(gsLocal_1, "headImgPath");
//                    resultTopList.Gender = MUTIL.GET_JSON_STRING(gsLocal_1, "Gender");      // 男=1女=0
//                    resultTopList.latitude = MUTIL.GET_JSON_STRING(gsLocal_1, "latitude");
//                    resultTopList.longitude = MUTIL.GET_JSON_STRING(gsLocal_1, "longitude");
//                    gsLocal_2 = MUTIL.GET_JSON_STRING(gsLocal_1, "City");
//                    if (!"".equals(gsLocal_2) && !"null".equals(gsLocal_2)) {
//                        String[] params = gsLocal_2.split(" ");
////MDWORD(0xd1);MSTRING(gsLocal_2);MSTRING(params[0]);
//                        if (params[0] != null && !"".equals(params[0])) {
//                            resultTopList.Province = params[0].replace("省", "");
//                        }
////MDWORD(0xd2);MSTRING(params[1]);
//                        if (params[1] != null &&!"".equals(params[1])) {
//                            resultTopList.City = params[1].replace("市", "");
//                        }
////MSTRING("0xd5 " + resultTopList.UserName +"&"+ resultTopList.UserIdS +"&"+ resultTopList.Gender +"&"+ resultTopList.Province +"&"+ resultTopList.City + "&" + resultTopList.latitude+ "&"+resultTopList.longitude +"&"+ resultTopList.HeadUrl);
//                    }
//                }
//
//                resultTopLists.add(resultTopList);
//            } while (++g4Local_2 < g4Local_1);
//            resultNet.result = resultTopLists;
//            gsLocal_2 = String.format("打开演唱汇：%s榜，找到用户%d个", aux1, g4Local_2);
//            MSTRING(gsLocal_2);
//        } catch (JSONException e) {
//            e.printStackTrace();
////MDWORD(0xbb);
//        }
//    }
//    }

    public void MSTRING(String string) {
        Log.d("MSYS", string);
    }
    public void MDWORD(int g4Local_1) {
        Log.d("MSYS", String.format("%08X", g4Local_1));
    }}
